﻿using System.Globalization;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Specialized;
using System.Windows.Data;
using System.ComponentModel;
using System.Reflection;


namespace FRL.DebtChased.ChaseWorkspace
{
    public class DateTimeToYearConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            DateTime date = (DateTime)value;
            return date.ToShortDateString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string strValue = value.ToString();
            DateTime resultDateTime;
            if (DateTime.TryParse(strValue, out resultDateTime))
            {
                return resultDateTime;
            }
            return value;
        }
    }

    /*
    public class InvoiceStatusToTextConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string status;
            CachedData.Singleton.InvoiceStatuses.TryGetValue(System.Convert.ToInt32(value), out status);
            return status;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            foreach (KeyValuePair<int, string> kvp in CachedData.Singleton.InvoiceStatuses)
            {
                if (kvp.Value == (string)value)
                    return kvp.Key;
            }
            return 0;
        }
    }
    /**/

    public class PointTypeConverter : IValueConverter
    {
        private static Dictionary<int, string> _pointTypes;
        private static Dictionary<int, string> PointTypes
        {
            get
            {
                if (_pointTypes == null)
                {
                    _pointTypes = new Dictionary<int, string>();
                    Type enumType = typeof(PointType);
                    FieldInfo[] Info = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
                    foreach (FieldInfo fi in Info)
                    {
                        int id = (int)fi.GetRawConstantValue();
                        string name = Enum.Parse(enumType, fi.Name, true).ToString();
                        _pointTypes.Add(id, name);
                    }

                };
                return _pointTypes;
            }
        }

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string result = null;
            if (value is int)
                PointTypes.TryGetValue((int)value, out result);
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            foreach (KeyValuePair<int, string> kvp in PointTypes)
                if (kvp.Value == value) return kvp.Key;
            return null;
        }
    }


    public class ViewVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value is bool && (bool)value == true) ? Visibility.Collapsed : Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


    public class ContactPointProvider
    {

        public static ObservableCollection<string> _types;
        public static ObservableCollection<string> Types
        {
            get
            {
                if (_types == null)
                {
                    Type enumType = typeof(PointType);
                    _types = new ObservableCollection<string>();
                    FieldInfo[] Info = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
                    foreach (FieldInfo fi in Info)
                    {
                        if ((int)fi.GetRawConstantValue() > 0)
                        {
                            string name = Enum.Parse(enumType, fi.Name, true).ToString();
                            _types.Add(name);
                        }
                    }

                };
                return _types;
            }
        }

        public ContactPointProvider()
        {
        }
    }

    public enum PointType
    {
        NotSet = 0,
        DDI = 1,
        Phone = 2,
        Mobile = 3,
        Fax = 4,
        Email = 5
    }


}
